/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package com.linktco.prison.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.linktco.common.utils.U;
import com.linktco.prison.entity.CabinetUser;
import com.linktco.prison.service.ICabinetUserService;
import com.linktco.prison.vo.CabinetUserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 档案人员信息表 控制器
 *
 * @author BladeX
 * @since 2024-12-23
 */
@RestController
@AllArgsConstructor
@RequestMapping("/cabinet-user")
@Api(value = "档案人员信息表", tags = "档案人员信息表接口")
public class CabinetUserController extends BladeController {

	private final ICabinetUserService cabinetUserService;


	@GetMapping("/detail")
 	@ApiOperation(value = "详情", notes = "传入cabinetUser")
	public R<CabinetUser> detail(CabinetUser cabinetUser) {
		CabinetUser detail = cabinetUserService.getOne(Condition.getQueryWrapper(cabinetUser));
		return R.data(detail);
	}



	@GetMapping("/page")
 	@ApiOperation(value = "分页", notes = "传入cabinetUser")
	public R<IPage<CabinetUserVO>> page(CabinetUserVO cabinetUser, Query query) {
		IPage<CabinetUserVO> pages = cabinetUserService.selectCabinetUserPage(Condition.getPage(query), cabinetUser);
		return R.data(pages);
	}


	@GetMapping("/exclude-page")
	@ApiOperation(value = "分页", notes = "传入query")
	public R<IPage<CabinetUserVO>> excludePage(CabinetUserVO cabinetUser ,Query query) {
		IPage<CabinetUserVO> pages = Condition.getPage(query);
		if (cabinetUser.getExclude()){
			pages = cabinetUserService.excludePage(pages, cabinetUser);
		}else if (U.notEmpty(cabinetUser.getUserIds())){
			pages = cabinetUserService.selectCabinetUserPage(pages, cabinetUser);
		}
		return R.data(pages);
	}


	@PostMapping("/save")
 	@ApiOperation(value = "新增", notes = "传入cabinetUser")
	public R save(@Valid @RequestBody CabinetUser cabinetUser) {
		CabinetUser one = cabinetUserService.getOne(Wrappers.<CabinetUser>lambdaQuery()
				.eq(CabinetUser::getIdCard, cabinetUser.getIdCard()));
		Assert.isNull(one,"该人员身份证已存在！");
		return R.status(cabinetUserService.save(cabinetUser));
	}


	@PostMapping("/update")
 	@ApiOperation(value = "修改", notes = "传入cabinetUser")
	public R update(@Valid @RequestBody CabinetUser cabinetUser) {
		CabinetUser one = cabinetUserService.getOne(Wrappers.<CabinetUser>lambdaQuery()
				.eq(CabinetUser::getIdCard, cabinetUser.getIdCard())
				.ne(CabinetUser::getId,cabinetUser.getId()));
		Assert.isNull(one,"该人员身份证已存在！");
		return R.status(cabinetUserService.updateById(cabinetUser));
	}


	@PostMapping("/remove")
 	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(cabinetUserService.deleteLogic(Func.toLongList(ids)));
	}

	
}
